package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.ItemTransferAction;
import gatperdut.chato.core.interpreter.grammar.comminfo.ItemTransferInfo;
import gatperdut.chato.core.item.ItemController;
import gatperdut.chato.core.item.inventory.InventoryController;

public class PutAction extends ItemTransferAction {

    public PutAction( ItemTransferInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        if ( !checkUnindexedWord( getInfo().getWhat() ) ) {
            return CommandExecutionResult.OK;
        }

        synchronized ( getEntityController().getInventoryController() ) {
            ItemController pick = getEntityController().getInventoryController().getHeldItemController( getInfo().getWhat() );
            if ( pick == null ) {
                getEntityController().tx( "You don't seem to be holding that." );
                return CommandExecutionResult.OK;
            }

            ItemController container = null;
            InventoryController inventoryControllerContainerIsIn = null;
            if ( !getInfo().isGround() ) {
                inventoryControllerContainerIsIn = getEntityController().getInventoryController();
            }
            if ( getInfo().isGround() || ( !getInfo().isGround() && inventoryControllerContainerIsIn == null ) ) {
                inventoryControllerContainerIsIn = getEntityController().getRoomController().getInventoryController();
            }
            synchronized ( inventoryControllerContainerIsIn ) {
                container = findInRoomOrInventory( getInfo().getIn(), getInfo().isGround() );
                if ( container == null ) {
                    getEntityController().tx( "You cannot find that container." );
                    return CommandExecutionResult.OK;
                }
                if ( container.getInventoryController() == null ) {
                    getEntityController().tx( container.getShortDesc() + " is not a container." );
                    return CommandExecutionResult.OK;
                }
                if ( !container.getItem().getInventory().isOpen() ) {
                    getEntityController().tx( container.getShortDesc() + " is closed." );
                    return CommandExecutionResult.OK;
                }

                synchronized ( getEntityController().getInventoryController() ) {
                    getEntityController().getInventoryController().takeFromInventory( pick );

                    container.getInventoryController().putInInventory( pick );

                    getEntityController().getEchoes().txPut( pick, container, emote );
                }
            }
        }
        return CommandExecutionResult.OK;
    }

}
